package com.anda.jsu.yangsp.core.persistence;

import java.io.Serializable;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Repository;

import com.anda.jsu.yangsp.core.persistence.generic.HibernateDaoSupportBean;

/**
 * 
 * @author xiaolu
 * @since 2012-12-06
 *
 */
@Repository("baseDao")
public class BaseDaoImpl extends HibernateDaoSupportBean implements IBaseDao{

	public void delete(Object o) {
		this.getHibernateTemplate().delete(o);
	}

	public void deleteAll(Collection lists) {
		this.getHibernateTemplate().deleteAll(lists);
	}

	public void deleteObject(Class clazz, Serializable id) {
		Object o = this.getHibernateTemplate().get(clazz, id);
		if(o != null){
			this.getHibernateTemplate().delete(o);
		}
	}

	public Object findById(Class clazz, Serializable id) {
		return this.getHibernateTemplate().get(clazz, id);
	}

	public List listAll(Class clazz) {
		return this.getHibernateTemplate().loadAll(clazz);
	}

	public List listByQuery(final String hql) {
		return this.getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session) throws HibernateException, SQLException {
				Query query = session.createQuery(hql);
				return query.list();
			}
		});
	}

	public List listBySQLQuery(final String sql) {
		return this.getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session) throws HibernateException,SQLException {
				Query query = session.createSQLQuery(sql);
				return query.list();
			}
		});
	}

	public void save(Object o) {
		this.getHibernateTemplate().save(o);
	}

	public void saveAll(Collection lists) {
		this.getHibernateTemplate().saveOrUpdateAll(lists);
	}

	public void saveOrUpdate(Object o) {
		this.getHibernateTemplate().saveOrUpdate(o);
	}

	public void update(Object o) {
		this.getHibernateTemplate().update(o);
	}

	public Integer updateByHql(String hql) {
		return this.getHibernateTemplate().bulkUpdate(hql);
	}

}
